// pages/wmm/pdd_new_index.js
const app = getApp();
var WxParse = require('../../wxParse/wxParse.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    centerHeight: 0,
    current: 1,
    list: [],
    hhh: 0,
    showModal: false,
    imgsrc: '',
    price1: 0,
    price2: 0,
    app_id: '',
    articleMsg: [],
    app_path: '',
    times: 0

  },
  //获取文章内容
  getMsg() {
    var that = this;
    wx.request({
        url: app.globalData.api + '/app.php?c=Article&a=getArticleMsg',
        data: {
            article_id: 36,
        },
        header: {
            "Content-Type": "application/x-www-form-urlencoded",
            'platform': '' + app.globalData.platform,
            'version': '' + app.globalData.version,
        },
        method: 'POST',
        dataType: 'json',
        success: function (res) {
            console.log(res);
            if (res.data.code == 0) {
                var article = res.data.data.article_msg.content;
                WxParse.wxParse('article', 'html', article, that, 5);
            } else {
                wx.showToast({
                    title: '' + res.data.msg,
                    icon: 'none',
                    duration: 1000
                })
            }

        },
        fail: function () {

        }
    });
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      centerHeight: wx.getSystemInfoSync().screenHeight - 175-40
    })
    this.getTopCatList()
    this.getMsg()
    
  },
  guize: function () {
    wx.navigateTo({
      url: 'guize',
    })
  },
  getTopCatList() {
    wx.showLoading({
      title: '加载中',
      mask: false
    })
    var that = this;
    wx.request({
      url: app.globalData.api + '/app.php?c=Pdd&a=getFreeGoodsList',
      data: {
        token: app.globalData.token,
        p: 1,
        per: 100
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {

        console.log('ppp');
        wx.hideLoading();

        if (res.data.code == 0) {
          var priceQuan;
          for(var i=0;i<res.data.data.list.length;i++){
            
            res.data.data.list[i].after=(res.data.data.list[i].zk_final_price-res.data.data.list[i].coupon_amount).toFixed(2)
          }
          that.setData({
            list: res.data.data.list,
          })
        } else {
          wx.showToast({
            title: '' + res.data.message,
            icon: 'none'
          })
          that.setData({
            list: []
          })
        }

      },
      fail: function () {
        wx.hideLoading();
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  btn: function (e) {
    console.log(e)
    this.setData({
      showModal: true
    })
  },
  getQuan: function (e) {
    var that = this;
    if (e.currentTarget.dataset.id.inventory == 0) {
      wx.showToast({
        title: '已售完！',
        icon: 'none',
        duration: 2000 //持续的时间
      })
    } else {
      wx.showLoading({
        title: '加载中',
        mask: false
      })
    wx.request({
      url: app.globalData.api + '/app.php?c=Pdd&a=getGoodsUrl',
      data: {
        token: app.globalData.token,
        goods_id: e.currentTarget.dataset.id.goods_id
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {
        wx.hideLoading();
        if (res.data.code == 0) {
          that.setData({
            showModal: true,
            imgsrc: that.data.list[e.currentTarget.dataset.index].pict_url,
            price1: that.data.list[e.currentTarget.dataset.index].subsidy_amount,
            price2: that.data.list[e.currentTarget.dataset.index].subsidy_amount,
            app_id: res.data.data.url_list[0].we_app_info.app_id,
            app_path: res.data.data.url_list[0].we_app_info.page_path
          })

        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000 //持续的时间
          })
        }

      },
      fail: function () {
        wx.hideLoading();
      }
    });

    }

  },
  toorder: function () {
    wx.navigateTo({
      url: 'orderList',
    })
  },
  // 禁止屏幕滚动
  preventTouchMove: function () {},
  cancla: function () {
    this.setData({
      showModal: false
    })
  },
  // 弹出层里面的弹窗
  ok: function () {
    wx.navigateToMiniProgram({
      appId: this.data.app_id,
      path: this.data.app_path,
      success(res) {
        console.log(res)
      }
    })
    this.setData({
      showModal: false
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.request({
      url: app.globalData.api + '/app.php?c=User&a=getUserMsg',
      data: {
        token: app.globalData.token
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {

        if (res.data.code == 0) {
          that.setData({
            times: res.data.data.user_msg.pdd_free_num
          })
        } else {

        }

      },
      fail: function () {

      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  tabNav: function (e) {
    this.setData({
      current: e.currentTarget.dataset.index
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    if (e.from === 'button') {
      console.log("来自页面内转发按钮");
      return {
        title: '快来汇客熊小程序领取免单啦',
        desc: '',
        // imageUrl: "/static/images/splash.png",
        path: '/pages/live/splash?auth_code=' + app.globalData.auth_code // 路径，传递参数到指定页面。
      }
    }
  }
})